/**
 * @Copyright (c) 2005 
 * @All rights reserved.
 *
 *
 *
 * @file_name   Message.js
 * @version     1.0
 * @author      黄新泽
 * @date        2007-10-08 16:09:40
 */

/**
 * Class and Function List:
 * Function list:
 * - function onscrollOpration(ev)         
 * - initialize : function(id, msg, state) 
 * - showMessage : function()              
 * - removeMessage : function()            
 * - failure : function()                  
 * - isSet : function(arg)                 
 * Classes list:
 * - .Message
 */

if (!$('message_contain'))
{
	var elt = new Element("div",{id:"message_contain", 'class':"m_container"});
	document.body.appendChild(elt);

	/*
	foo =  Builder.node('div', {id:'message_contain', class:'m_container'}, 'sss')
	document.body.appendChild(foo);
	*/
}

function onscrollOpration(ev)
{
	$("message_contain").setStyle({top: (document.viewport.getScrollOffsets().top+22) +	"px"}); 
};

var	Message = Class.create({

	prefix : 'm_',
	info : {g:0, e:0, zIndex:500, ga:[], ea:[]},

	empty : function(){return false;},

	initialize : function(id, msg, state) 
	{
		this.messageState			= state						;	//是否显示提示消息
		this.eTarget				= id						;	//提示消息ID
		this.messageId				= id + new Date().getTime()	;	//提示消息ID
		this.loadMessage			= msg						;
		this.loadCount				= 0							;
	},

	showMessage : function()
	{
		if (!this.messageState)
		{
			return false;
		}

		this.loadCount++ ;

		if ('box' == this.messageState)
		{
			box.addMsgWindow();
		}
		else if ('mask' == this.messageState)
		{
			//this.mask(document.body, this.loadMessage);
			document.body.mask(this.loadMessage);
		}
		else if ('load' == this.messageState && $(this.eTarget))
		{
			//this.mask(this.eTarget, this.loadMessage);
			$(this.eTarget).mask(this.loadMessage);
		}
		else
		{
			var html='<div id="'+this.messageId+'" class="m_message"> '+this.loadCount+":"+this.loadMessage+'</div>';


			var msg= this.loadCount+":"+this.loadMessage;

			if(1 == this.loadCount)
			{
				$('message_contain').add({'top': html});
			}
			else
			{
				try	
				{
					$(this.messageId).edit(msg) ;
				}
				catch (e)
				{
					this.loadCount -= 2;
				}
				
			}

			html = msg = null;

			setTimeout(this.removeMessage.bind(this), 16000);
		}

		console.info(mask_index)
	},

	removeMessage : function()
	{
		if (!this.messageState)
		{
			return false;
		}

		if(this.loadCount < 1)
		{
			return ;
		}

		this.loadCount-- ;


		if ('box' == this.messageState)
		{
			mask.removeMsg();
		}
		else if ('mask' == this.messageState)
		{
			//this.unmask(document.body);
			document.body.unmask();
		}
		else if ('load' == this.messageState && $(this.eTarget))
		{
			//this.unmask(this.eTarget);
			$(this.eTarget).unmask();
		}
		else
		{
			if(this.loadCount < 1)
			{
				//alert(this.messageId);
				//Element.remove(this.messageId);
				if ($(this.messageId))
				{
					$(this.messageId).del(); 
				}
			}
			else
			{
				//alert(this.messageId+":>1");
				try	
				{
					var msg = this.loadCount + ":" + this.loadMessage ;
					$(this.messageId).edit(msg) ;
				}
				catch (e)
				{
					this.loadCount--;
				}
			}
		}
	},

	failure : function()
	{
		var eR = '';

		if (!isTrue($A(arguments)))
		{
			eR = _('failure');

			if (this.isSet(this.errorMessage))
			{
				eR = _('failure') + '<br />' + _('reason') + '：' + this.errorMessage; 
			}
		}
		else
		{
			eR = '';
		}

		if ('box' == this.messageState)
		{
		}
		else if ('mask' == this.messageState)
		{
		}
		else if ('load' == this.messageState && $(this.eTarget))
		{
		}
		else
		{
			if ($(this.messageId))
			{
				$(this.messageId).edit(this.loadCount + " : " + this.loadMessage + eR);
				//Effect.Shake($(this.messageId));
			}
		}

		//后面显示this.loadMessage,提示信息没有改变
		//setTimeout(this.removeMessage.bind(this), 500);
		this.removeMessage();

		eR = null;
	},

	m : function (msg) 
	{
		this.loadMessage= msg;
		this.showMessage();
		this.failure();

	},

	isSet : function(arg)
	{
		return 'undefined' != typeof(arg) ?	true : false;
	},


	/**
	 * 生成mask
	 *
	 * @access	public
	 */
	mask: function(id, msg)
	{
		var mask_id = this.prefix + id;

		if ($(id))
		{
			var elt = new Element("div",{id:mask_id});
			elt.setStyle({
				position:"absolute",

				width:$(id).getWidth() + "px",
				height:$(id).getHeight() + "px",
				left:$(id).cumulativeOffset().left + "px",
				top:$(id).cumulativeOffset().top + "px"});

			//elt.update(msg);
			elt.setOpacity(0.7);
			elt.addClassName ('blackWaiting');
			$(document.body).insert(elt);

			elt = null;
		}
		else
		{
			var elt=new Element("div",{id: mask_id});
			elt.setStyle({
				position:"absolute",

				width:document.viewport.getWidth()+"px",
				height:document.viewport.getHeight()+"px",
				left:document.viewport.getScrollOffsets().left+"px",
				top:document.viewport.getScrollOffsets().top+"px"});

			//elt.update(msg);
			elt.setOpacity(0.7);
			elt.addClassName ('blackWaiting');
			$(document.body).insert(elt);

			elt = null;
		}

		return mask_id;
	},

	/**
	 * 删除mask
	 *
	 * @access	public
	 */
	unmask: function(id)
	{
		if ($(id))
		{
			$(this.prefix + id).del();
		}
		else
		{
			$(this.prefix + id).del();
		}
	}
});

document.observe("dom:loaded", function()
{
	Event.observe(window, 'scroll', onscrollOpration, false);
})

//document.observe("dom:loaded", function()
//{
	//if (!$('message_contain'))
	//{
		//var message_contain_div = document.createElement('div');
		//Element.extend(message_contain_div);
		//message_contain_div.addClassName('m_container');
		//message_contain_div.writeAttribute('id', 'message_contain');

		//document.body.appendChild(message_contain_div);
	//}

	//Event.observe(window, 'scroll', onscrollOpration, false);

	//window.M = new Message('MHXZ', L_Global.Message['wrong'], true);
//});

